﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Supplier;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Supplier
{
    /// <summary>
    /// 联系人信息删除
    /// </summary>
    public class ContactPersonsDeleteHandler : IRequestHandler<ContactPersonsDeleteCommand, THE_WIND_RISES.Common.Dtos.Result>
    {
        /// <summary>
        /// 联系人信息
        /// </summary>
        private readonly IBaseRepository<ContactPersons> contactPersonsRepository;

        private readonly ILogger<ContactPersonsDeleteHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contactPersonsRepository"></param>
        public ContactPersonsDeleteHandler(IBaseRepository<ContactPersons> contactPersonsRepository, ILogger<ContactPersonsDeleteHandler> logger)
        {
            this.contactPersonsRepository = contactPersonsRepository;
            this.logger = logger;
        }
        public async Task<Result> Handle(ContactPersonsDeleteCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Delete };

            try
            {
                string sql = $"update ContactPersons set IsDelete=1 where ContactPersonsId={request.ContactPersonsId}";

                var list = contactPersonsRepository.Update(sql, result);

                if (list > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Msg = "删除成功";
                    logger.LogInformation("联系人信息删除");
                }
            }
            catch (Exception ex)
            {
                logger.LogError("联系人信息删除失败"+ex.Message);
                throw;
            }

            return result;
        }
    }
}
